<?php

namespace app\company\controller;

use app\common\controller\CompanyController;
use app\common\service\annotation\ControllerAnnotation;
use app\common\service\annotation\NodeAnnotation;
use app\company\model\CompanyUser;
use app\Request;
use think\App;
use think\response\Json;

/**
 * @ControllerAnnotation(title="个人中心")
 */
class Personal extends CompanyController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new CompanyUser();
    }

    /**
     * @NodeAnnotation(title="修改密码")
     * @param Request $request
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function changePassword(Request $request): Json
    {
        $post = $request->post();
        $rule = [
            'old_password|旧密码' => 'require',
            'new_password|新密码' => 'require',
            'again_password|再次确认密码' => 'require',
        ];
        $this->validate($post, $rule);
        $row = $this->model->find($this->uid);
        if (empty($row)) return $this->error('数据不存在！');
        if ($row->password != password($post['old_password'])) return $this->error('旧密码与原先不一致！');
        if ($post['new_password'] != $post['again_password']) return $this->error('两次输入密码不一致！');
        try {
            $this->model->transaction(function () use ($post, $row, &$save)
            {
                $save = $row->save(['password' => password($post['new_password'])]);
            });
        } catch (\Exception $e) {
            $this->error('修改失败:' . $e->getMessage());
        }
        if (!$save) return $this->error('修改失败');
        return $this->success('修改成功');
    }

    /**
     * @NodeAnnotation(title="修改头像")
     * @param Request $request
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function changeAvatar(Request $request): Json
    {
        $post = $request->post();
        $rule = [
            'head_img|头像' => 'require',
        ];
        $this->validate($post, $rule);
        $row = $this->model->find($this->uid);
        if (empty($row)) return $this->error('数据不存在！');
        try {
            $this->model->transaction(function () use ($post, $row, &$save)
            {
                $save = $row->save(['head_img' => $post['head_img']]);
            });
        } catch (\Exception $e) {
            $this->error('修改失败:' . $e->getMessage());
        }
        if (!$save) return $this->error('修改失败');
        return $this->success('修改成功');
    }

    /**
     * @NodeAnnotation(title="修改信息")
     * @param Request $request
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function changeMessage(Request $request): Json
    {
        $post = $request->post();
        $rule = [
            'phone|手机号' => 'require',
            'email|邮箱' => 'require',
        ];
        $this->validate($post, $rule);
        $row = $this->model->find($this->uid);
        if (empty($row)) return $this->error('数据不存在！');
        try {
            $this->model->transaction(function () use ($post, $row, &$save)
            {
                $save = $row->save(['phone' => $post['phone'], 'email' => $post['email']]);
            });
        } catch (\Exception $e) {
            $this->error('修改失败:' . $e->getMessage());
        }
        if (!$save) return $this->error('修改失败');
        return $this->success('修改成功');
    }
}